package cn.bigway.app.weavingdesigner.util;

import java.io.File;
import java.util.HashSet;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class WdFileChooser extends JFileChooser {

	public static final String EXT_IMAGE = "bmp,gif,jpg,jpeg,tiff,tif,png";

	private static final long serialVersionUID = 7624710432026362961L;

	public WdFileChooser(String ext) {
		setFileFilter(new WdFilter(ext));
	}
}

class WdFilter extends FileFilter {
	private String extNames;
	private HashSet<String> fileExts;

	public WdFilter(String ext) {
		this.fileExts = new HashSet<String>();
		this.extNames = (null == ext) ? "" : ext.toLowerCase().trim();
		String[] exts = this.extNames.split(",");
		for (String fileExt : exts) {
			if (fileExt != null) {
				this.fileExts.add(fileExt.trim());
			}
		}
	}

	@Override
	public boolean accept(File file) {
		if (file.isDirectory()) {
			return true;
		}
		for (String ext : this.fileExts) {
			if (file.getName().toLowerCase().endsWith(ext)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String getDescription() {
		return "文件类型为：" + this.extNames;
	}
}
